export default function ({ redirect, $axios, app, store
}) {


	let cookiesLg = app.$cookies.get('lg')
	let queryLg = app.context.query.lang
	console.log('queryLg==> ', queryLg);




	let that = this;
	// 数据访问前缀
	if (process.server) {
		// 获取服务端的token
		// var token = getCookie.getcookiesInServer(req).token;
		// console.log("tokenserver==>", token)
	}
	if (process.client) {
		// 获取客户端token
		// var token = getCookie.getcookiesInClient('token');
		// console.log("tokenclient==> ",token)
	}
	// 请求拦截器
	$axios.interceptors.request.use(
		(config) => {
			// 	let local = app.$cookies.get("auth.strategy");

			// 	let token = app.$cookies.get(`auth._token.${local}`);
			// 	if (token) {
			// 		config.headers.common["token"] = token.split(" ")[1];
			// 	}
			// cn  中，，   en 英
			let lg = queryLg || '';

			if (queryLg == 'cn') {
				lg = 'cn';
			}

			if (queryLg == 'en') {
				lg = 'en';
			}

			if (queryLg == 'zh') {
				lg = 'zh';
			}

			if (!queryLg) {
				if (cookiesLg) {
					if (cookiesLg == 'zh-CN') {
						lg = 'cn';
					} else if (cookiesLg == 'zh-TW' || cookiesLg == 'zh-HK') {
						lg = 'zh';
					} else {
						lg = 'en';
					}
				} else {
					if (navigator.language == 'zh-CN') {
						lg = 'cn';
					} else if (navigator.language == 'zh-TW' || navigator.language == 'zh-HK') {
						lg = 'zh';
					} else {
						lg = 'en';
					}
				}
			}

			const u = `?m=api&c=zb&_ajax=1&lang=${lg}`;
			config.url = u + config.url;
			return config;







		},
		(error) => {
			// do something with request error
			return Promise.reject(error);
		}
	);

	$axios.interceptors.response.use(
		(response) => {
			let code = response.data ? Number(response.data.code) : 0;
			// console.log('response.data==> ', response.data)
			switch (code) {
				case 200:
					return response.data;

				case 500:
				case 410000:
				case 410001:
				case 410002:
				case 40000:
				case 400:
					return Promise.reject(response.data.mesg);
				// if (response.data.message !== '请登录') {
				//   return Promise.reject(response.data.message)
				// } else {
				//   app.$cookies.set('fromPath', location.pathname + location.search)
				//   let local = app.$cookies.get('auth.strategy')
				//   app.$cookies.remove(`auth._token.${local}`)
				//   localStorage.clear()
				//   app.store.commit('isLogin', true)
				//   return Promise.reject(response.data.message)
				// }
				// break
				default:
					return response.data;
					break;
			}
		},
		(error) => {
			return Promise.reject(error);
		}
	);
}
